package com.hyphenate.easeui.widget.chatrow;

import android.app.Activity;
import android.content.Context;
import android.text.Spannable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import com.hyphenate.easeui.adapter.EaseMessageAdapter;
import com.hyphenate.easeui.adapter.ItemState;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ChatCard;
import com.joplus.housekeeper.bean.PlanRightNowBean;
import com.joplus.housekeeper.bean.PlanRightNowBean.DataBean.TravelPlanDTOBean.SerInfosBean;

import com.joplus.housekeeper.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * 即将要发生的行程
 */
public class EaseChatRowText_Plan_Now extends EaseChatRow {


    private ProgressBar progressBar;
    private LinearLayout layout_plan;
    private ImageView iv_weather;
    private TextView tv_place;
    private TextView tv_time;
    private TextView tv_foot_r;
    private TextView tv_foot_l;
    private TextView tv_head_r;
    private TextView tv_head_l;

    private TagFlowLayout id_flowlayout;

    private ImageView btn_switch;//扩展相关服务的按钮
    TagAdapter totalAdatpter;
    TagAdapter tempadapter;

    List<SerInfosBean> tempData;
    List<SerInfosBean> totalData;
    static Boolean isclicked = false;

    EaseMessageAdapter easeMessageAdapter;
    String highTemperature;
    String lowTemperature;
    String typeTemperature;

    ChatCard chatCard;

    static PlanRightNowBean planRightNowBean;

    public EaseChatRowText_Plan_Now setHighTemperature(String highTemperature) {
        this.highTemperature = highTemperature;
        return this;
    }

    public EaseChatRowText_Plan_Now setLowTemperature(String lowTemperature) {
        this.lowTemperature = lowTemperature;
        return this;
    }

    public EaseChatRowText_Plan_Now setTypeTemperature(String typeTemperature) {
        this.typeTemperature = typeTemperature;
        return this;
    }

    public EaseChatRowText_Plan_Now(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    public EaseChatRowText_Plan_Now(Context context, EMMessage message, int position, BaseAdapter adapter, int i) {
        super(context);
        this.context = context;
        this.activity = (Activity) context;
        this.message = message;
        this.position = position;
        this.adapter = adapter;
        inflater = LayoutInflater.from(context);
        easeMessageAdapter = (EaseMessageAdapter) adapter;
        super.initView();

    }


    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message_plan_now : R.layout.ease_row_sent_message, this);

        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容

        String msg = txtBody.getMessage();
        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            totalData = chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getSerInfos();
            tempData = new ArrayList<>();
            if (totalData.size() > 2) {
                for (int i = 0; i < 2; i++) {
                    tempData.add(totalData.get(i));
                }

            } else {
                for (int i = 0; i < totalData.size(); i++) {
                    tempData.add(totalData.get(i));
                }
            }

        } catch (Exception e) {

            // bubbleLayout.setVisibility(View.VISIBLE);


        }


        tempadapter = new TagAdapter<SerInfosBean>(tempData) {
            @Override
            public View getView(FlowLayout parent, int position, final SerInfosBean o) {
                LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.layout_card_expend_message,
                        id_flowlayout, false);

                TextView serName = (TextView) tv.findViewById(R.id.tv_serName);
                ImageView serIco = (ImageView) tv.findViewById(R.id.tv_serIco);
                serName.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                ImageLoader.getInstance().displayImage(o.getSerIcon(), serIco);

                StringUtils.setText(serName, o.getSerName());
                return tv;
            }
        };
        totalAdatpter = new TagAdapter<SerInfosBean>(totalData) {
            @Override
            public View getView(FlowLayout parent, int position, final SerInfosBean o) {

                LinearLayout tv = (LinearLayout) inflater.inflate(R.layout.layout_card_expend_message,
                        id_flowlayout, false);
                TextView serName = (TextView) tv.findViewById(R.id.tv_serName);
                ImageView serIco = (ImageView) tv.findViewById(R.id.tv_serIco);
                serName.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                ImageLoader.getInstance().displayImage(o.getSerIcon(), serIco);

                StringUtils.setText(serName, o.getSerName());
                return tv;
            }
        };
    }

    @Override
    protected void onFindViewById() {
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        layout_plan = (LinearLayout) findViewById(R.id.layout_plan);
        tv_head_l = (TextView) findViewById(R.id.tv_head_l);
        tv_head_r = (TextView) findViewById(R.id.tv_head_r);
        tv_foot_l = (TextView) findViewById(R.id.tv_foot_l);
        tv_foot_r = (TextView) findViewById(R.id.tv_foot_r);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_place = (TextView) findViewById(R.id.tv_place);
        iv_weather = (ImageView) findViewById(R.id.iv_weather);
        id_flowlayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
        tv_head_r.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chatCard.getPlanRightNowBean() != null && chatCard.getPlanRightNowBean().getData() != null &&
                        chatCard.getPlanRightNowBean().getData().getTravelPlanDTO() != null &&
                        chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelName() != null &&
                        chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelDate() != null &&
                        chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelAddress() != null) {
                    String name = chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelName();
                    String date = chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelDate();
                    String address = chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelAddress();

                }

            }
        });


        btn_switch = (ImageView) findViewById(R.id.btn_switch);
        btn_switch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemStateChangeListener != null) {
                    itemStateChangeListener.onChange(position, ItemState.Trigger);
                }
                btn_switch.setVisibility(GONE);
                id_flowlayout.setAdapter(totalAdatpter);
            }
        });
        if (totalData != null && totalData.size() <= 2) {
            btn_switch.setVisibility(GONE);
        }
        if (easeMessageAdapter != null && easeMessageAdapter.itemStates.get(position).getState() != ItemState.Normal) {
            btn_switch.setVisibility(GONE);
            id_flowlayout.setAdapter(totalAdatpter);
        } else {
            id_flowlayout.setAdapter(tempadapter);
        }

        if (chatCard.getPlanRightNowBean() != null &&
                chatCard.getPlanRightNowBean().getData().getTravelPlanDTO() != null &&
                chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelName() != null &&
                chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelDate() != null &&
                chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelAddress() != null) {
            tv_head_l.setText(chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelName());
            tv_time.setText(chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelDate());
            tv_place.setText(chatCard.getPlanRightNowBean().getData().getTravelPlanDTO().getTravelAddress());
        }

    }

    @Override
    protected void onUpdateView() {

    }

    @Override
    protected void onSetUpView() {
//        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
//        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
//        // 设置内容
//        tv_chatcontent.setText(span, TextView.BufferType.SPANNABLE);
//        String msg = txtBody.getMessage();
//
//        try {
//            new JSONObject(msg);
//            Gson gson = new Gson();
//            tv_chatcontent.setVisibility(View.GONE);
//            bubbleLayout.setVisibility(View.GONE);
//            layout_plan.setVisibility(VISIBLE);
//
//        } catch (Exception e) {
//            tv_chatcontent.setVisibility(VISIBLE);
//            bubbleLayout.setVisibility(View.VISIBLE);
//            layout_plan.setVisibility(GONE);
//
//        }
//       handleTextMessage();

    }

    @Override
    protected void onBubbleClick() {

    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == EMMessage.ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void insertCard(final Context context, String travelPlanId, final EaseChatRowText_RecommandCard.InsertRecommandCard i) {



    }

    public static interface InsertRecommandCard {
        void insertMessage(String message);
    }
}
